package State
{
	import ResourcePackage.ExternalResource;
	import ResourcePackage.ResourceMovieClip;
	
	import core.MatchDTO;
	
	import flash.display.MovieClip;
	import flash.events.*;
	import flash.net.*;
	import flash.text.*;
	import flash.utils.ByteArray;
	
	import flashx.textLayout.formats.TextAlign;
	
	
	public class GSColosseum extends State
	{
		

		[Embed(source="../rsc/interface.swf", mimeType="application/octet-stream")]
		public static var InterfaceSprite:Class;
		public var itfSprite:ResourceMovieClip = new ResourceMovieClip();
		
		private var m_match:MatchDTO = new MatchDTO();;
		private var m_bSetMatched:Boolean;
		
		private var m_gameTime:int;
		private var m_gameTicker:int;
		private var m_LastRefresh:int;
		public function GSColosseum()
		{
			super();
		}
		
		public override function Render(g:MyGraphics):void
		{
			Game.GetMyGraphics().SetTextAlign(TextAlign.LEFT);
			Game.GetMyGraphics().SetTextSize(16);
			Game.GetMyGraphics().DrawString(CGame.GetGame().m_playerName,5,5,0x0000ff);
			
			Game.GetMyGraphics().DrawString(CGame.GetGame().m_sessionKey,5,55,0x0000ff);
			
			
		}
		public override function Update(dt:int):void
		{
			if(CGame.GetGame().m_bIsConnecting)
				return;
			m_gameTicker += dt;
			if(m_gameTicker >=1000)
			{
				m_gameTime += m_gameTicker/1000;
				m_gameTicker = m_gameTicker%1000;
			}
			
			var post:FBHttpPost;
			var data:String
			//refresh list each 5 second
			if(m_bSetMatched == true && m_gameTime % 5 == 0 && m_LastRefresh != m_gameTime)
			{
				post = new FBHttpPost(CallBack_ListMatch);				
				post.Post(Define.URLListMatch,"time=" + m_gameTime);
				CGame.GetGame().m_bIsConnecting = true;
				m_LastRefresh = m_gameTime;
			}
			
			if(m_bSetMatched && m_match.PlayerName1 !="" && m_match.PlayerName2 != "")
			{
				m_match.reset();
				
				
				GSBattle.s_match = m_match;
				var state:State = CGame.CreateState(3);				
				if(state)
				{
					Game.GetStateStack().SwitchState(state,false);	
				}
				return;
			}
			
			var btnID:int;
			for(btnID = 0;btnID<2;btnID++)
			{
				if(CGame.GetGame().m_keypad.HasKeyBeenReleased(btnID))
				{					
					if(m_bSetMatched && CGame.GetGame().m_keypad.GetButton(btnID).GetText() == CGame.GetGame().m_playerName)
					{
						//CGame.GetGame().m_keypad.GetButton(btnID).SetText("");
						//m_bSetMatched = false;						
						post = new FBHttpPost(CallBack_JoinMatch);
						data = "matchid=" + m_match.id + "&turn=" + btnID + "&usrname=" + CGame.GetGame().AuthenticateKey();
						post.Post(Define.URLJoinMatch,data);
						CGame.GetGame().m_bIsConnecting = true;
					}
					else if(!m_bSetMatched && CGame.GetGame().m_keypad.GetButton(btnID).GetText() == "")
					{
						//CGame.GetGame().m_keypad.GetButton(btnID).SetText(CGame.GetGame().m_playerName);
						//m_bSetMatched = true;
						
						post = new FBHttpPost(CallBack_JoinMatch);
						data = "matchid=" + m_match.id + "&turn=" + btnID + "&usrname=" + CGame.GetGame().m_playerName  + CGame.GetGame().AuthenticateKey();
						post.Post(Define.URLJoinMatch,data);
						CGame.GetGame().m_bIsConnecting = true;
					}
				}
			}
		}
		public override function Init():void
		{

			
			var s:Vector.<ExternalResource> = new Vector.<ExternalResource>();
			
			s.push(new ExternalResource(itfSprite, ExternalResource.TYPE_FromByteArray, new InterfaceSprite()));
			
			Game.GetStateStack().Push(new StateLoading("Init",s,this, false, false));
			//Game.GetMainWindow().addChild(itfSprite.GetMovieClip().frameButton1);
							
			m_bSetMatched = false;
			m_LastRefresh = 0;
		}
		
		public function PostInit():void
		{
			
			CGame.GetGame().m_keypad.AddButton(0,100,100,itfSprite.GetMovieClip().CreatetButton1(),"");
			CGame.GetGame().m_keypad.AddButton(1,250,100,itfSprite.GetMovieClip().CreatetButton1(),"");
			
			var post:FBHttpPost = new FBHttpPost(CallBack_ListMatch);
			//var data:String = "username=" + loginRscMovieClip.GetMovieClip().username.text + "&password=" + loginRscMovieClip.GetMovieClip().password.text;
			post.Post(Define.URLListMatch,"time=" + m_gameTime);
			CGame.GetGame().m_bIsConnecting = true;
			m_gameTime = 0;
			m_gameTicker = 0;
			m_LastRefresh = m_gameTime;
			
		}
		public override function Release():void
		{
			CGame.GetGame().m_keypad.RemoveButton(0);
			CGame.GetGame().m_keypad.RemoveButton(1);
			CGame.GetGame().m_touchInterfMgr.ClearAll();
		}
	
		public override function ReceiveMessage(mess:String,param:Array,sender:MovieClip):void
		{
			
		}

		public override function LoadComplete(success:Boolean, description:String):void
		{
			if(success)
			{
				switch(description)
				{
					case "Init":
						 
	
						PostInit();
						break;
					
				}
			}
		}
		public override function GetStateName():String
		{
			return "GSColosseum";
		}
		
		public function CallBack_JoinMatch(data:String,b:Boolean):void
		{
			CGame.GetGame().m_bIsConnecting = false;
			
			if(b == false)
			{
				//Game.GetStateStack().Push(new StatePopup(data,"OK"));
				return;
			}

			var variables:URLVariables = new URLVariables(data);
			if (variables.error != null)
			{
				var state:State = CGame.CreateState(1);
				if(state)
				{
					Game.GetStateStack().SwitchState(state,false);	
				}
			}
			else if (variables.matchid > 0)
			{
				m_match.id = variables.matchid;
				//m_match.state = variables.matchid;
				m_match.PlayerName1 = variables.playername1;
				m_match.PlayerName2 = variables.playername2;
				
				if(m_match.PlayerName1 == CGame.GetGame().m_playerName || m_match.PlayerName2 == CGame.GetGame().m_playerName)
					m_bSetMatched = true;
				else
					m_bSetMatched = false;
				
				CGame.GetGame().m_keypad.GetButton(0).SetText(m_match.PlayerName1);
				CGame.GetGame().m_keypad.GetButton(1).SetText(m_match.PlayerName2);
			}
		}
		public function CallBack_ListMatch(data:String,b:Boolean):void
		{
			CGame.GetGame().m_bIsConnecting = false;
			if(b == false)
			{
				//Game.GetStateStack().Push(new StatePopup(data,"OK"));
				return;
			}

			var variables:URLVariables = new URLVariables(data);
			if (variables.matchid > 0)
			{
				m_match.id = variables.matchid;
				//m_match.state = variables.matchid;
				m_match.PlayerName1 = variables.playername1;
				m_match.PlayerName2 = variables.playername2;
				
				if(m_match.PlayerName1 == CGame.GetGame().m_playerName || m_match.PlayerName2 == CGame.GetGame().m_playerName)
					m_bSetMatched = true;
				else
					m_bSetMatched = false;
				
				CGame.GetGame().m_keypad.GetButton(0).SetText(m_match.PlayerName1);
				CGame.GetGame().m_keypad.GetButton(1).SetText(m_match.PlayerName2);
			}
		}
	}
}